<?php

/**
 * customer_care actions.
 *
 * @package    sf_sandbox
 * @subpackage customer_care
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class customer_careActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
    $this->forward('default', 'module');
  }
  
  public function executeResendSms()
  {
  	try {
  		$this->err = 0; //success
  		$this->id = $this->getRequestParameter('id',0);
	  	$this->ringDownload = RingtoneDownloadPeer::retrieveByPK($this->id);
	  	$ringId = $this->ringDownload->getRingtoneId();
	  	$phoneFrom  = $this->ringDownload->getFromPhone();
	  	$phoneTo = $this->ringDownload->getToPhone();
	  	$price = $this->ringDownload->getPrice();
	  	$downloadDate = $this->ringDownload->getDownloadDatetime();
	  	//send SMS
	  	$days = round((strtotime(date('Y-m-d H:i:s')) - strtotime($downloadDate)) /24 /60 /60);
	  	if($days <= 14)
	  	{
		  	if($phoneFrom == $phoneTo)
		  	{
		  		$sms =  ringtoneLib::resendSmsDownload(1, $ringId, $phoneTo, $phoneFrom, $price);
		  	}
		  	else 
		  	{
		  		$sms =  ringtoneLib::resendSmsDownload(2, $ringId, $phoneTo, $phoneFrom, $price);	
		  	}
		  	
		  	if ($sms[0] == null |$sms[0] != 0){//Phat sinh loi trong qua trinh gui tin nhan
		  		sfContext::getInstance()->getLogger()->err("SMS SENT ERROR : $sms[1],From: $phoneFrom, To: $phoneTo, Content: $ringId");
		  		$this->err = 1;
		  	}
		  	else{
		  		sfContext::getInstance()->getLogger()->err("SMS SENT SUCCESS : $sms[1],From: $phoneFrom, To: $phoneTo, Content: $ringId");
		  	}
		  	//update sms_count
		  	$this->sms_count = RingtoneDownloadPeer::updateSmsCount($this->id);
	  	
	  	}
	  	else 
	  	{
	  		$this->err = 3;
	  	}
	  	
	  	
  	} catch (Exception $e) {
  		$this->err = 2;
  	}
  }
  
	function count_days( $a, $b )
	{
	    // First we need to break these dates into their constituent parts:
	    $gd_a = getdate( $a );
	    $gd_b = getdate( $b );
	
	    // Now recreate these timestamps, based upon noon on each day
	    // The specific time doesn't matter but it must be the same each day
	    $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
	    $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
	
	    // Subtract these two numbers and divide by the number of seconds in a
	    //  day. Round the result since crossing over a daylight savings time
	    //  barrier will cause this time to be off by an hour or two.
	    return round( abs( $a_new - $b_new ) / 86400 );
	}
}
